﻿using Common.Contract;
using Common.Contract.Customer;
using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request.PortfolioManagement;
using Common.Contract.Customer.Response.PortfolioManagement;
using Common.Contract.Paging;
using Common.Service;
using Common.Service.DataTables;
using Common.Type;
using SMECustomerWebsite.Core.Configuration;
using SMECustomerWebsite.Models.ViewModels.PortfolioManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Common.Util;
using Common.Contract.Customer.Request;
using Common.Contract.Customer.Response;
using Common.Contract.Authentication;
using Common.Contract.Authentication.Request;
using Common.Contract.Authentication.Response;
using Common.Contract.Authentication.Record;
using System.Globalization;
using Common.Contract.SystemConfig.Record;
using SMECustomerWebsite.Filters;

namespace SMECustomerWebsite.Controllers
{
    public class PortfolioManagementController : ControllerBase
    {
        [CustomActionFilter]
        [Authorize]
        public ActionResult AccountPlanning()
        {
            AccountPlanningViewModel vm = new AccountPlanningViewModel
            {
                UserRecord = CurrentUserRecord,
            };
            return View(vm);
        }

        [Authorize]
        [HttpPost]
        public ActionResult AccountPlanningData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);

                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        filter.PaymentMethod = pageRequest.SearchColumns[7];
                        filter.StatusAccPlan = pageRequest.SearchColumns[8];

                        break;
                    case "2":
                        filter.PaymentMethod = pageRequest.SearchColumns[6];
                        filter.StatusAccPlan = pageRequest.SearchColumns[7];
                        break;
                    case "3":
                        filter.PaymentMethod = pageRequest.SearchColumns[5];
                        filter.StatusAccPlan = pageRequest.SearchColumns[6];
                        break;
                    case "4":
                        filter.PaymentMethod = pageRequest.SearchColumns[3];
                        filter.StatusAccPlan = pageRequest.SearchColumns[4];
                        break;
                    default:
                        break;
                }
                RetrieveAccountPlanningRequest request = new RetrieveAccountPlanningRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.PortfolioManagementController.AccountPlanningData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    UserRecord = CurrentUserRecord,
                    TextSearch = filter,
                    Lang = CurrentLang
                };
                var customerRecords = new RetrieveAccountPlanningResponse();
                customerRecords = serviceClient.Proxy.RetrieveAccountPlanning(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<AccountPlanningRecord>() : customerRecords.Records;
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "4":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Cif,
                                                             x.CustName,
                                                             x.Industry,
                                                             x.PaymentMethod,
                                                             x.Status.Equals("0")? SMECustomerWebsite.Core.Resources.Resource.NotYet : SMECustomerWebsite.Core.Resources.Resource.Inputed,
                                                             x.DaoCif
                    }).ToList()
                        };
                        break;
                    case "3":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.DaoCif,
                                                             x.DaoName,
                                                             x.Cif,
                                                             x.CustName,
                                                             x.Industry,
                                                             x.PaymentMethod,
                                                             x.Status.Equals("0")? SMECustomerWebsite.Core.Resources.Resource.NotYet : SMECustomerWebsite.Core.Resources.Resource.Inputed,
                                                             x.DaoCif
                    }).ToList()
                        };
                        break;
                    case "2":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.BranchName,
                                                             x.DaoCif,
                                                             x.DaoName,
                                                             x.Cif,
                                                             x.CustName,
                                                             x.Industry,
                                                             x.PaymentMethod,
                                                             x.Status.Equals("0")? SMECustomerWebsite.Core.Resources.Resource.NotYet : SMECustomerWebsite.Core.Resources.Resource.Inputed,
                                                             x.DaoCif
                    }).ToList()
                        };
                        break;
                    case "1":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.RegionName,
                                                             x.BranchName,
                                                             x.DaoCif,
                                                             x.DaoName,
                                                             x.Cif,
                                                             x.CustName,
                                                             x.Industry,
                                                             x.PaymentMethod,
                                                             x.Status.Equals("0")? SMECustomerWebsite.Core.Resources.Resource.NotYet : SMECustomerWebsite.Core.Resources.Resource.Inputed,
                                                             x.DaoCif
                    }).ToList()
                        };
                        break;
                    default:
                        break;
                }

            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult DaoInfo(string dao)
        {
            UserRecord resultCustomer = new UserRecord();
            using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
            {
                RetrieveUserByDaoRequest request = new RetrieveUserByDaoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.PortfolioManagementController.DaoInfo"
                    },
                    Dao = dao,
                    CurrentDao = CurrentUserRecord
                };
                var customerRecords = new RetrieveUserResponse();
                customerRecords = serviceClient.Proxy.RetrieveUserByDao(request);
                resultCustomer = (customerRecords == null || customerRecords.Record == null) ? new UserRecord() : customerRecords.Record;
            }
            var objectItem = new
            {
                daoName = resultCustomer.UserName,
                position = resultCustomer.ContractType,
                dao = dao,
                email = resultCustomer.Email,
                branch = resultCustomer.Branch,
                region = resultCustomer.Zone,
                startwork = resultCustomer.StartWorkingDate,
            };
            return Json(objectItem);
        }
        #region Edit by Cif
        [CustomActionFilter]
        [Authorize]
        public ActionResult EditAccountPlanning(string cif)
        {
            AccountPlanningRecord record = new AccountPlanningRecord();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveAccountPlanningByCifRequest request = new RetrieveAccountPlanningByCifRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.PortfolioManagementController.EditAccountPlanning"
                    },
                    Cif = cif,
                };
                var customerRecords = new RetrieveAccountPlanningByCifResponse();
                customerRecords = serviceClient.Proxy.RetrieveAccountPlanningByCif(request);
                record = customerRecords.Records.FirstOrDefault();
            }

            AccountPlanningViewModel vm = new AccountPlanningViewModel
            {
                CustName = record.CustName,
                Cif = cif,
                MainSector = record.MainSector,
                PaymentMethod = record.PaymentMethod,
                ActionPlanT62016 = record.ActionPlanT62016,
                WalletShare = record.WalletShare,
                SpBanCheo = record.SpBanCheo,
                KhHoTro = record.KhHoTro,
                VuongMac = record.VuongMac,
            };
            return View(vm);
        }

        [Authorize]
        [HttpPost]
        public ActionResult EditAccountPlanning(AccountPlanningViewModel vm, FormCollection form)
        {
            try
            {
                using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
                {
                    ResponseRecord response = null;
                    UpdateAccountPlanningRequest request = new UpdateAccountPlanningRequest
                    {
                        Header = new RequestHeaderRecord
                        {
                            ApplicationKey = SystemConfiguration.ApplicationKey,
                            CallerName = "SMEsCustomer360Degree.Controllers.PortfolioManagementController.EditAccountPlanning"
                        },
                        Record = new AccountPlanningRecord
                        {
                            Cif = vm.Cif,
                            ActionPlanT62016 = vm.ActionPlanT62016,
                            MainSector = vm.MainSector,
                            PaymentMethod = vm.PaymentMethod,
                            WalletShare = vm.WalletShare,
                            SpBanCheo = vm.SpBanCheo,
                            VuongMac = vm.VuongMac,
                            KhHoTro = vm.KhHoTro,
                            DaoCif = CurrentUser
                        }
                    };
                    response = serviceClient.Proxy.UpdateAccountPlanning(request);
                    if (response.IsSuccess)
                    {
                        return RedirectToAction("AccountPlanning", "PortfolioManagement");
                    }
                    else
                    {
                        vm.Message = response.ErrorMessage;
                        vm.MessageType = MessageType.Error;
                        //vm.IsSupperAdmin = CurrentUser.Equals(SystemConfiguration.SUPPER_ADMIN, StringComparison.OrdinalIgnoreCase);
                    }
                    //record = customerRecords.Records.FirstOrDefault();
                }
            }
            catch (Exception ex)
            {
                vm.MessageType = MessageType.Error;
                vm.Message = ex.ToString();
            }
            return View(vm);
        }

        [CustomActionFilter]
        [Authorize]
        public ActionResult EditAccountPlanningFinance(string cif)
        {
            List<DataAccountPlanningRecord> record = new List<DataAccountPlanningRecord>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveDataAccountPlanningByCifRequest request = new RetrieveDataAccountPlanningByCifRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.PortfolioManagementController.EditAccountPlanningFinance"
                    },
                    Cif = cif,
                };
                var customerRecords = new RetrieveDataAccountPlanningByCifResponse();
                customerRecords = serviceClient.Proxy.RetrieveAccountPlanningFinanceByCif(request);
                record = customerRecords.Records;
            }

            DataAccountPlanningViewModel vm = new DataAccountPlanningViewModel
            {
                listDataAccountPlanning = record
            };
            return View(vm);
        }
        [Authorize]
        [HttpPost]
        public ActionResult EditAccountPlanningFinanceAction(string dataedit, string cif)
        {
            bool isUpdate = false;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                ResponseRecord response = null;
                UpdateDataAccountPlanningRequest request = new UpdateDataAccountPlanningRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.PortfolioManagementController.EditAccountPlanningFinanceAction"
                    },
                    DataUpdate = dataedit,
                    DaoChange = CurrentUser,
                    Cif = cif
                };
                response = serviceClient.Proxy.UpdateAccountPlanningFinance(request);
                isUpdate = response.IsSuccess;
            }
            return Json(isUpdate);
        }

        [CustomActionFilter]
        [Authorize]
        public ActionResult EditProductHolding(string cif)
        {
            List<DataAccountPlanningRecord> record = new List<DataAccountPlanningRecord>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveDataAccountPlanningByCifRequest request = new RetrieveDataAccountPlanningByCifRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.PortfolioManagementController.EditProductHolding"
                    },
                    Cif = cif,
                };
                var customerRecords = new RetrieveDataAccountPlanningByCifResponse();
                customerRecords = serviceClient.Proxy.RetrieveAccountPlanningProductHoldingByCif(request);
                record = customerRecords.Records;
            }

            DataAccountPlanningViewModel vm = new DataAccountPlanningViewModel
            {
                listDataAccountPlanning = record
            };
            return View(vm);
        }
        [Authorize]
        [HttpPost]
        public ActionResult EditProductHoldingAction(string dataedit, string cif)
        {
            bool isUpdate = false;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                ResponseRecord response = null;
                UpdateDataAccountPlanningRequest request = new UpdateDataAccountPlanningRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.PortfolioManagementController.EditProductHoldingAction"
                    },
                    DataUpdate = dataedit,
                    DaoChange = CurrentUser,
                    Cif = cif
                };
                response = serviceClient.Proxy.UpdateAccountPlanningProductHolding(request);
                isUpdate = response.IsSuccess;
            }
            return Json(isUpdate);
        }

        #endregion

        #region View by Cif
        [CustomActionFilter]
        [Authorize]
        public ActionResult ViewDataAccountPlanning(string cif)
        {
            AccountPlanningRecord record = new AccountPlanningRecord();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveAccountPlanningByCifRequest request = new RetrieveAccountPlanningByCifRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.PortfolioManagementController.ViewDataAccountPlanning"
                    },
                    Cif = cif,
                };
                var customerRecords = new RetrieveAccountPlanningByCifResponse();
                customerRecords = serviceClient.Proxy.RetrieveAccountPlanningByCif(request);
                record = customerRecords.Records.FirstOrDefault();
            }

            AccountPlanningViewModel vm = new AccountPlanningViewModel
            {
                CustName = record.CustName,
                Cif = cif,
                TinhHinhKhaiThac = record.TinhHinhKhaiThac,
                Plan2016 = record.Plan2016,
                SpDangSuDung = record.SpDangSuDung,
                ThuPhi2016 = record.ThuPhi2016,
                TyLeSuDungHanMuc = record.TyLeSuDungHanMuc,
                MainSector = record.MainSector,
                PaymentMethod = record.PaymentMethod,
                ActionPlanT62016 = record.ActionPlanT62016,
                WalletShare = record.WalletShare,
                SpBanCheo = record.SpBanCheo,
                KhHoTro = record.KhHoTro,
                VuongMac = record.VuongMac,
            };
            return View(vm);
        }

        [CustomActionFilter]
        [Authorize]
        public ActionResult AccountPlanningFinance(string cif)
        {
            List<CustomerInfoRecord> resultCustomer = new List<CustomerInfoRecord>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerInfoRequest request = new RetrieveCustomerInfoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.PortfolioManagementController.AccountPlanningFinance"
                    },
                    Cif = cif
                };
                var customerRecords = new RetrieveCustomerInfoResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerInfoByCif(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerInfoRecord>() : customerRecords.Records;
            }
            AccountPlanningDataViewModel vm = new AccountPlanningDataViewModel
            {
                Cif = cif,
                CustName = resultCustomer.FirstOrDefault().CustName
            };
            return View(vm);
        }
        [Authorize]
        [HttpPost]
        public ActionResult AccountPlanningFinanceData(DataTablesPageRequest pageRequest, string cif)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                int index = pageRequest.SearchColumns.FindIndex(x => !string.IsNullOrEmpty(x));
                RetrieveAccountPlanningDataViewRequest request = new RetrieveAccountPlanningDataViewRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.PortfolioManagementController.AccountPlanningFinanceData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    TimeSearch = pageRequest.Search.IsNullOrEmpty() ? DateTime.Now.Month : int.Parse(pageRequest.Search),
                    Cif = cif
                };
                CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
                var customerRecords = new RetrieveAccountPlanningDataViewResponse();
                customerRecords = serviceClient.Proxy.RetrieveAccountPlanningFinanceDataViewByTime(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<AccountPlanningDataViewRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.Group,
                                                             x.Portfolio,
                                                             x.Plan.Equals("0")?x.Plan:float.Parse(x.Plan).ToString("#,###", cul.NumberFormat),
                                                             x.Result.Equals("0")?x.Result:float.Parse(x.Result).ToString("#,###", cul.NumberFormat),
                                                             (float.Parse(x.Result)-float.Parse(x.Plan)).ToString("#,###", cul.NumberFormat),
                                                             x.Plan.Equals("0")?"100" :(float.Parse(x.Result)/float.Parse(x.Plan)*100).ToString()
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }
        [CustomActionFilter]
        [Authorize]
        public ActionResult ProductHolding(string cif)
        {
            List<CustomerInfoRecord> resultCustomer = new List<CustomerInfoRecord>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerInfoRequest request = new RetrieveCustomerInfoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.PortfolioManagementController.ProductHolding"
                    },
                    Cif = cif
                };
                var customerRecords = new RetrieveCustomerInfoResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerInfoByCif(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerInfoRecord>() : customerRecords.Records;
            }
            AccountPlanningDataViewModel vm = new AccountPlanningDataViewModel
            {
                Cif = cif,
                CustName = resultCustomer.FirstOrDefault().CustName
            };
            return View(vm);
        }
        [Authorize]
        [HttpPost]
        public ActionResult ProductHoldingData(DataTablesPageRequest pageRequest, string cif)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                int index = pageRequest.SearchColumns.FindIndex(x => !string.IsNullOrEmpty(x));
                RetrieveAccountPlanningDataViewRequest request = new RetrieveAccountPlanningDataViewRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.PortfolioManagementController.ProductHoldingData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    TimeSearch = pageRequest.Search.IsNullOrEmpty() ? DateTime.Now.Month : int.Parse(pageRequest.Search),
                    Cif = cif
                };
                var customerRecords = new RetrieveAccountPlanningDataViewResponse();
                customerRecords = serviceClient.Proxy.RetrieveAccountPlanningProductHoldingDataViewByTime(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<AccountPlanningDataViewRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.Group,
                                                             x.Portfolio,
                                                             x.Plan,
                                                             x.Result,
                                                             x.Result.Equals("Y")?"Done": x.Plan==x.Result?"Don't have plan":"Not done"
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }
        #endregion

        #region History Edit
        [CustomActionFilter]
        [Authorize]
        public ActionResult HistoryEdit(string cif)
        {
            List<AccountPlanningHistoryRecord> record = new List<AccountPlanningHistoryRecord>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveAccountPlanningHistoryRequest request = new RetrieveAccountPlanningHistoryRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.PortfolioManagementController.HistoryEdit"
                    },
                    Cif = cif,
                };
                var customerRecords = new RetrieveAccountPlanningHistoryResponse();
                customerRecords = serviceClient.Proxy.RetrieveAccountPlanningHistoryByCif(request);
                record = customerRecords.Records;
            }
            List<CustomerInfoRecord> resultCustomer = new List<CustomerInfoRecord>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerInfoRequest request = new RetrieveCustomerInfoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.PortfolioManagementController.HistoryEdit"
                    },
                    Cif = cif
                };
                var customerRecords = new RetrieveCustomerInfoResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerInfoByCif(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerInfoRecord>() : customerRecords.Records;
            }
            AccountPlanningHistoryViewModel vm = new AccountPlanningHistoryViewModel
            {
                CustName = resultCustomer.FirstOrDefault().CustName,
                Cif = cif,
                ListHistory = record
            };
            return View(vm);
        }

        [CustomActionFilter]
        [Authorize]
        public ActionResult ViewHistoryFinance(string cif, string type, string id)
        {
            var vm = GetHistoryViewModel(cif, type, id);
            return View(vm);
        }

        [CustomActionFilter]
        [Authorize]
        public ActionResult ViewHistoryProductHolding(string cif, string type, string id)
        {
            var vm = GetHistoryViewModel(cif, type, id);
            return View(vm);
        }
        [CustomActionFilter]
        [Authorize]
        public ActionResult ViewHistoryData(string cif, string type, string id)
        {
            var vm = GetHistoryViewModel(cif, type, id);
            return View(vm);
        }
        [Authorize]
        private AccountPlanningHistoryViewModel GetHistoryViewModel(string cif, string type, string id)
        {
            List<AccountPlanningHistoryRecord> record = new List<AccountPlanningHistoryRecord>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveAccountPlanningHistoryRequest request = new RetrieveAccountPlanningHistoryRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.PortfolioManagementController.GetHistoryViewModel"
                    },
                    Cif = cif,
                    Type = type,
                    Id = id
                };
                var customerRecords = new RetrieveAccountPlanningHistoryResponse();
                customerRecords = serviceClient.Proxy.RetrieveAccountPlanningHistoryByCifType(request);
                record = customerRecords.Records;
            }
            AccountPlanningHistoryViewModel vm = new AccountPlanningHistoryViewModel
            {
                Cif = cif,
                ListHistory = record
            };
            return vm;
        }

        [Authorize]
        [ChildActionOnly]
        public ActionResult CifInfo(string cif)
        {
            List<CustomerInfoRecord> resultCustomer = new List<CustomerInfoRecord>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerInfoRequest request = new RetrieveCustomerInfoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.PortfolioManagementController.CifInfo"
                    },
                    Cif = cif
                };
                var customerRecords = new RetrieveCustomerInfoResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerInfoByCif(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerInfoRecord>() : customerRecords.Records;
            }
            AccountPlanningViewModel vm = new AccountPlanningViewModel
            {
                Cif = cif,
                CustName = resultCustomer.FirstOrDefault().CustName
            };
            return PartialView("_CifInfo", vm);
        }
        #endregion

        [CustomActionFilter]
        [Authorize]
        public ActionResult PortfolioManagement()
        {
            return View();
        }

    }
}
